package com.rk.kbk.controller;

import com.alibaba.fastjson.JSONArray;
import com.rk.kbk.common.JsonResp;
import com.rk.kbk.common.PubLib;
import com.rk.kbk.common.SqlToResult;
import com.rk.kbk.dao.DynamicPlaceDao;
import com.rk.kbk.model.DynamicPlace;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by bilibili on 2017/4/21.
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/dynamicPlace")
public class DynamicPlaceController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private DynamicPlaceDao dynamicPlaceDao;
    @Autowired
    private SqlToResult sqlToResult;

    /**
     * @骑手实时地址
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/riderDynamicPlace",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp riderDynamicPlace(String areaCode) {
        log.debug("骑手实时地址");
        //得到骑手实时地址
        String sql=" SELECT c.* FROM(SELECT a.* FROM dynamic_place a INNER JOIN (SELECT MAX(id) id,ride_id FROM dynamic_place GROUP BY ride_id)b ON a.ride_id=b.ride_id AND a.id=b.id WHERE a.area_code="+areaCode+" ORDER BY a.ride_id) as c INNER JOIN user u WHERE c.ride_id=u.id and u.is_work=1 and u.user_status=1 ";
        List<DynamicPlace> resultList = (List<DynamicPlace>) sqlToResult.getResultList(sql);
        List<Map> list = new ArrayList<>();
        for (Object dynamicPlace : resultList) {
            Map map = PubLib.copyObject(dynamicPlace, Map.class);
            list.add(map);
        }
        //找到所有骑手id
        List<Long> ids= new ArrayList<>();
        for (Map m : list) {
            String value=String.valueOf(m.get("rideId"));
            Long id =Long.parseLong(value);
            ids.add(id);
        }
        //根据id得到当前订单数
        List<Map> orderlist = new ArrayList<>();
        for(Long l:ids){
            String sqltwo="SELECT count(id) as num,IFNULL(ride,"+l+") as rideId  FROM orders WHERE ride = "+l+" and order_status = 3   ";
            Object resultListtwo = sqlToResult.getResultList(sqltwo, "one");
            Map map = PubLib.copyObject(resultListtwo, Map.class);
            orderlist.add(map);
        }
        //合并
        for(int i=0;i<list.size();i++){
            for (int j=0;j<orderlist.size();j++){
                if(list.get(i).get("rideId").equals(orderlist.get(j).get("rideId"))){
                    list.get(i).put("num",orderlist.get(j).get("num"));
                }
            }
        }
        //根据id得到姓名
        List<Map> namelist = new ArrayList<>();
        for(Long l:ids){
            String sqlthree="SELECT * FROM `user` WHERE id = "+l+" ";
            Object resultListthree = sqlToResult.getResultList(sqlthree, "one");
            Map map = PubLib.copyObject(resultListthree, Map.class);
            namelist.add(map);
        }
        //合并id
        for(int i=0;i<list.size();i++){
            for (int j=0;j<orderlist.size();j++){
                if(list.get(i).get("rideId").equals(namelist.get(j).get("id"))){
                    list.get(i).put("rideName",namelist.get(j).get("realName"));
                }
            }
        }
        return JsonResp.ok(list);

    }

}
